//
//  ChancePhotoQualityViewController.m
//  图片故事
//
//  Created by 大帅逼 on 16/3/3.
//  Copyright © 2016年 helloApp. All rights reserved.
//

#import "ChancePhotoQualityViewController.h"
#import "Header.h"
#import "PhotoQualitTableViewCell.h"
#import "LocaData.h"
#import "ChanceQualityTableViewCell.h"
#import "MBProgressHUD+PKX.h"

@interface ChancePhotoQualityViewController ()<UITableViewDelegate, UITableViewDataSource>{

    NSInteger seletedRow;
}

@property (nonatomic, strong) UITableView *photoTableView;
@property (nonatomic, strong) UIView      *footerView;

@end

@implementation ChancePhotoQualityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view setBackgroundColor:[UIColor blackColor]];
    
    [self initNavigation];
    
    [self.view addSubview:self.photoTableView];
}

- (void)initNavigation {
    
    self.title = @"图片质量";
    
    UIBarButtonItem *backItme = [[UIBarButtonItem alloc] initWithTitle:@"返回 " style:UIBarButtonItemStylePlain target:self action:@selector(action_backItmeClickEvent)];
    
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    
    self.navigationItem.leftBarButtonItem = backItme;
    
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor], NSFontAttributeName : [UIFont systemFontOfSize:20]}];
}

#pragma mark -- 点击事件
- (void)action_backItmeClickEvent {
    
    [self.navigationController popToRootViewControllerAnimated:YES];
}

#pragma mark -- tableView 代理方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if(seletedRow == [indexPath row])
        return;
    seletedRow = [indexPath row];
    
    [tableView  reloadData];
    
    // 将图片质量保存
    [LocaData setPhotoQualitWithIndex:indexPath.row];
    
    [self.navigationController popToRootViewControllerAnimated:YES];
}

#pragma mark -- tableView 数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    ChanceQualityTableViewCell *photoCell = [tableView dequeueReusableCellWithIdentifier:@"Photo_Cell"];
    
    if (!photoCell) {
        
        photoCell = [[ChanceQualityTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Photo_Cell"];
    }
    
    if ([LocaData readPhotoQualit] == indexPath.row) {
        [photoCell setAccessoryType:UITableViewCellAccessoryCheckmark];
        
    } else {
        [photoCell setAccessoryType:UITableViewCellAccessoryNone];
        
    }
    
    [photoCell setPhotoModel:[[LocaData readSetPhotoQualit] objectAtIndex:indexPath.row]];
    
    [photoCell setBackgroundColor:[UIColor clearColor]];
    
    return photoCell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {

    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return My_Height(60);
}

#pragma mark -- 懒加载
- (UITableView *)photoTableView {
    
    if (!_photoTableView) {
        
        _photoTableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0, SCREEN_W, SCREEN_H)];
        
        [_photoTableView setDataSource:self];
        
        [_photoTableView setDelegate:self];
        
        [_photoTableView setSeparatorColor:[UIColor colorWithWhite:0.4 alpha:1]];
        
        [_photoTableView setBackgroundColor:[UIColor colorWithWhite:0.6 alpha:0.2]];
        
        [_photoTableView setTableFooterView:self.footerView];
        
    }
    return _photoTableView;
}

- (UIView *)footerView {
    
    if (!_footerView) {
        
        _footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, My_Height(50))];
        
        UIImageView *errorView = [[UIImageView alloc] initWithFrame:CGRectMake(My_Height(20), My_Height(5), My_Height(30), My_Height(30))];
        
        [errorView setImage:[UIImage imageNamed:@"error_msg"]];
        
        [_footerView addSubview:errorView];
        
        UILabel *msgLabel = [[UILabel alloc] initWithFrame:CGRectMake(My_Height(50), My_Height(10), SCREEN_W - My_Height(50), My_Height(20))];
        
        [msgLabel setText:@"设置图片质量，会撤销当前图片的所有设置"];
        
        [msgLabel setFont:[UIFont systemFontOfSize:12]];
        
        [msgLabel setTextColor:[UIColor colorWithWhite:0.8 alpha:1]];
        
        [_footerView addSubview:msgLabel];
    }
    
    return _footerView;
}

@end
